package com.lianzhihui.romeex.ui.me;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.google.android.material.tabs.TabLayout;
import com.lianzhihui.romeex.MyApplication;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.config.AppConstants;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.ui.me.fragment.AddRecodeFragment;
import com.lianzhihui.romeex.ui.me.fragment.UseRecodeFragment;
import com.lianzhihui.romeex.ui.mianfragment.TradeFragment;
import com.lianzhihui.romeex.ui.trade.fragment.ExchangeFragment;
import com.lianzhihui.romeex.ui.trade.fragment.FlowCellFragment;
import com.lianzhihui.romeex.utils.StatusBarUtil;
import com.lianzhihui.romeex.widget.MyViewPager;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhouyou.http.EasyHttp;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: romeex
 * @Package: com.lianzhihui.romeex.ui.me
 * @ClassName: BindPhoneActivity
 * @Description:  额度记录
 * @Author: zhanghong
 * @CreateDate: 2020/11/2 15:50
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/11/2 15:50
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class CreditRecordActivity extends BaseActivity implements TabLayout.OnTabSelectedListener, OnRefreshListener {

    private ArrayList<Fragment> fragmentList;
    private MyFragmentAdapter adapter;
    private FragmentManager fragmentManager;
    @BindView(R.id.status_bar_id)
    View statusBarId;
    @BindView(R.id.include_top_iv_back)
    ImageView includeTopIvBack;
    @BindView(R.id.include_top_lin_newback)
    LinearLayout includeTopLinNewback;
    @BindView(R.id.include_top_tv_title)
    TextView includeTopTvTitle;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.include_top_iv_right_second)
    ImageView includeTopIvRightSecond;
    @BindView(R.id.include_top_tv_right)
    TextView includeTopTvRight;
    @BindView(R.id.include_top_lin_right)
    LinearLayout includeTopLinRight;
    @BindView(R.id.include_top_lin_background)
    LinearLayout includeTopLinBackground;
    @BindView(R.id.app_top_bar)
    LinearLayout appTopBar;
    @BindView(R.id.header_layout)
    LinearLayout headerLayout;

    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.mViewPager)
    MyViewPager mViewPager;
    @BindView(R.id.main_wallet_oc_lin_main)
    RelativeLayout mainWalletOcLinMain;
    @BindView(R.id.mySmartRefreshLayout)
    SmartRefreshLayout mySmartRefreshLayout;
    private int position = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_credit_record;
    }

    @Override
    protected void initView() {
        StatusBarUtil.setTransparent(this);
        StatusBarUtil.setStatusBarTextColor(this, false);
        setTitle(getResources().getString(R.string.text_exchange_recode_balance));
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            tvBalance.setText(extras.getString(AppConstants.INTENT_EXCHANGE_BALANCE, ""));
        }
        includeTopLinBackground.setBackgroundColor(getResources().getColor(R.color.transparent));
        fragmentList = new ArrayList<>();
        fragmentList.add(new UseRecodeFragment());
        fragmentList.add(new AddRecodeFragment());
        fragmentManager = getSupportFragmentManager();
        adapter = new MyFragmentAdapter(fragmentManager, fragmentList);
        mViewPager.setScrollable(false);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setAdapter(adapter);
        tabLayout.addOnTabSelectedListener(this);
        mySmartRefreshLayout.setOnRefreshListener(this);
//        mySmartRefreshLayout.autoRefresh();
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        position = tab.getPosition();
        mViewPager.setCurrentItem(position);
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        if (position == 1) {
            ((UseRecodeFragment) fragmentList.get(0)).onRefresh();
        } else {
            ((AddRecodeFragment) fragmentList.get(1)).onRefresh();
        }
    }

    public void ChildFragmentFinished() {
        mySmartRefreshLayout.finishRefresh();
    }


    public static class MyFragmentAdapter extends FragmentPagerAdapter {
        private List<Fragment> fragments;

        public MyFragmentAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }


        @Override
        public int getCount() {
            return fragments.size();
        }
    }


    @OnClick({R.id.include_top_iv_back})
    public void OnClick(View v) {
        switch (v.getId()) {

            case R.id.include_top_iv_back:
                finish();
                break;

            default:
                break;
        }
    }


}
